'use strict';
const CONFIG = require('../../../common/config');
const CODE = require('../../../common/code');
const CommonUtil = require('../../../common/util/CommonUtil');



const codeUtil = function () {

    function codeUtil() {
    }

    /**
     * 生成推广码
     */
    codeUtil.genPromoCode = function(){
        let ret = null;

        const cur = CommonUtil.genCurYearDayTimeStr();
        const p0 = CommonUtil.randomNumStr(2); // 2位随机数字
        const p1 = cur.substr(8); // 分秒
        const p2 = "";// index:001 [缩短长度 暂不使用]
        let p3 = cur.substr(0,8);// 年月日时
        p3 = CommonUtil.yearDayTimeStrtoAlphabet(p3); // 4位26进制字符串
        const tmp = Number(p0+p1+p2)||0;
        if(tmp && tmp>0) {
            let c1 = tmp.toString(36).toUpperCase();// 36进制
            //while (c1.length<6) {
            //    c1 = "0"+c1;
            //}
            ret = c1+p3;
        }

        return ret;
    };

    /**
     * 生成默认昵称
     *
     * @author 王俊
     * @date 2017-07-19
     *
     * @param {Object} [options]   请求参数
     * @param {String} [options.prefix] 前缀, 默认为 'yjx_'
     * @param {String} [options.prefix_length] 前缀, 默认为 4
     * @return {string}
     */
    codeUtil.genDefaultNickName = function (options) {
        const timeStamp = Date.parse(new Date()) / 1000;
        const  startTimeTamp = new Date(new Date().setHours(0, 0, 0, 0)) / 1000;
        let dateNum =timeStamp-startTimeTamp;
        dateNum = ( "000000000000" + dateNum ).substr(-5);
        if(options && options.prefix) {
            return options.prefix + dateNum;
        }
        return "yjx_" + dateNum;
    };

    /**
     * 创建订单号
     *
     * @author 王俊
     * @date 2018-07-22
     *
     * @param {Object} options 配置参数
     * @param {Number} [options.p00] 终端: 4:pc, 5:ios ,6:android, 7:微信嵌入h5, 8:小程序, 9:app嵌入h5
     * @param {Number} [options.p01] 类型: 1:普通订单, 2:发票订单, 6:转账(Itb转出)订单
     * @return {string} tradeNo
     */
    codeUtil.genTradeNo = function (options) {
        const cur = CommonUtil.genCurYearDayTimeStr();
        const  p00 = options.p00 || 0; // 标志
        const  p01 = options.p01 || 1; // 暂时默认为1
        const  p0 = p00+""+p01;
        const  p1 = cur.substr(8); // 分秒
        const  p2 = CommonUtil.randomNumStr(3); // 3位随机数
        let  p3 = cur.substr(0,8);// 年月日时
        p3 = CommonUtil.yearDayTimeStrtoAlphabet(p3); // 4位26进制字符串
        const  ret = CommonUtil.mix(p0+p1+p2, p3);
        return ret;
    };

    return codeUtil;
}();

module.exports = codeUtil;